Hì hục mãi thì cuối cùng cũng chạy (code em tham khảo và chỉnh sửa chút xíu ) . Mọi thứ oki , nhưng chỉ có điều là thời gian nó chạy chậm hơn thời gian thực vài s/phút . Xắp đến ngày nộp đồ án môn mà em vẫn chưa khác phục đươc . Nên post lên đây nhờ sự giúp đỡ của các bác . Bác nào rảnh dành chút thời gian check hộ em với Em cám ơn
Đây là code và mô phỏng ạ :
DO AN VDK.rar
Đây là code và mô phỏng ạ :
DO AN VDK.rar
Code:
/* Project: Dong Ho Thoi Gian Thuc Hien Thi 8 Led 7 Doan Nhóm : Vinh-Tuan-Tuan GVHD : Vu Thi Thu Huong */ #include <REGX52.H> #define Menu P3_0 //phim menu de chon che do chinh thoi gian #define Next P3_1 //phim next de tang thoi gian #define Back P3_2 //phim back de giam thoi gian #define Timer_Date P3_3 //phim de chuyen che do hien thi #define SCL P3_5 #define SDA P3_6 #define LedCPU P0_0 #define OffLed {P1=0x00,P2=0xFF;} //dinh nghia dia chi cac thanh ghi trong DS1307 #define SEC 0x00 #define MIN 0x01 #define HOUR 0x02 #define DATE 0x03 #define DAY 0x04 #define MONTH 0x05 #define YEAR 0x06 //cac ham dung cho DS1307 void I2C_Start(); //khoi dong cho DS1307 void I2C_stop(); unsigned char I2C_write(unsigned char dat); //ham ghi 1 byte du kieu vao DS1307 unsigned char I2C_read(); //ham doc 1 byte du kieu vao DS1307 void DS1307_Read(unsigned char addr); //ham doc du lieu tu 1 thanh ghi bat ky void DS1307_write(unsigned char addr,unsigned char dat); //ham ghi du lieu tu 1 thanh ghi bat ky void Write_DS1307(); //ham cai dat thoi gian vao DS1307 void Read_DS1307(); //ham doc thoi gian vao DS1307 void Convert_Number(); //ham chuyen doi tach so de hien thi tren led 7 thanh void Display_Gio_Phut_Giay(); //ham hien thi gio phut giay tren led 7 thanh void Display_Ngay_Thang_Nam(); //ham hien thi ngay thang nam void ScanKey(); //ham quet phim nhan //khai bao bien unsigned char Mang[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; unsigned char SoGioHangChuc,SoGioHangDonVi,SoPhutHangChuc,SoPhutHangDonVi,SoGiayHangChuc,SoGiayHangDonVi; unsigned char SoNgayHangChuc,SoNgayHangDonVi,SoThangHangChuc,SoThangHangDonVi,SoNamHangChuc,SoNamHangDonVi; unsigned int Millisec,Millisec2; //bien dem ms char sec,hour,min,day,date,month,year; //cac bien dem gio phut giay.... unsigned char kt_sec; //bien kiem tra xem RTC co hoat dong khong unsigned char kt_sec2; unsigned char kt_menu; //bien vao cac che do chinh thoi gian bit kt_Timer; //kiem tra trang thai hien thi //cac ham khai bao void delay_ms(unsigned int ms) { unsigned int i,j; for(i=0;i<ms;i++) for(j=0;j<121;j++); } void ISR_1000ms(void) interrupt 1 // ham ngat timer 0 :1ms { TF0 = 0;//xoa co tran TH0 =0xe5; // nap du lieu vao thanh ghi timer TL0 =0xf8; TR0=1; Millisec++; Millisec2++; if(Millisec==0xFFFF) Millisec=0; if(Millisec%1000==0) LedCPU=~LedCPU; } //ham quet led hien thi gio phut giay void Display_Gio_phut_giay() { P1=1; // led thu nhat sang P2=Mang[SoGioHangChuc]; //xuat du lieu ra led thu nhat delay_ms(2); //tre 1ms P1=2; P2=Mang[SoGioHangDonVi]; delay_ms(2); P1=4; P2=0xBF; delay_ms(2); P1=8; P2=Mang[SoPhutHangChuc]; delay_ms(2); P1=16; P2=Mang[SoPhutHangDonVi]; delay_ms(2); P1=32; P2=0xBF; delay_ms(2); P1=64; P2=Mang[SoGiayHangChuc]; delay_ms(2); P1=128; P2=Mang[SoGiayHangDonVi]; delay_ms(2); OffLed; } //ham quet led hien thi ngay thang nam void Display_Ngay_Thang_Nam() { P1=1; P2=Mang[SoNgayHangChuc]; delay_ms(2); P1=2; P2=Mang[SoNgayHangDonVi]; delay_ms(2); P1=4; P2=0xBF; delay_ms(2); P1=8; P2=Mang[SoThangHangChuc]; delay_ms(2); P1=16; P2=Mang[SoThangHangDonVi]; delay_ms(2); P1=32; P2=0xBF; delay_ms(2); P1=64; P2=Mang[SoNamHangChuc]; delay_ms(2); P1=128; P2=Mang[SoNamHangDonVi]; delay_ms(2); OffLed; } void Convert_Number() { SoNgayHangChuc=day/10; SoNgayHangDonVi=day%10; SoThangHangChuc=month/10; SoThangHangDonVi=month%10; SoNamHangChuc=year/10; SoNamHangDonVi=year%10; SoGiayHangChuc=sec/10; SoGiayHangDonVi=sec%10; SoPhutHangChuc=min/10; SoPhutHangDonVi=min%10; SoGioHangChuc=hour/10; SoGioHangDonVi=hour%10; } void ScanKey() { if(Menu==0)//neu phim menu duoc nhan { delay_ms(500); kt_menu++; //bien luu trang thai menu hien tai Write_DS1307();//cu moi lan nhan phim thi phai ghi du lieu vao DS1307 if(kt_menu==7) { kt_menu=0; //tro ve trang thai hien thi thoi gian binh thuong } while(Menu==0);//cho khi nao nha phim menu } if(Timer_Date==0) { delay_ms(20); kt_Timer=~kt_Timer; while(Timer_Date==0); } if(kt_menu!=0) { switch(kt_menu) { case 1: //chinh thoi gian ngay { if(Back==0) { delay_ms(20); day--; if(day<=0) day=31; while(Back==0); } else if(Next==0) { delay_ms(20); day++; if(day>=32) day=1; while(Next==0); } P1=1; P2=Mang[SoNgayHangChuc]; delay_ms(1); P1=2; P2=Mang[SoNgayHangDonVi]; delay_ms(1); OffLed; } break; case 2: //chinh thang { if(Back==0) { delay_ms(20); month--; if(month<=0) month=12; while(Back==0); } else if(Next==0) { delay_ms(20); month++; if(month>=13) month=1; while(Next==0); } P1=8; P2=Mang[SoThangHangChuc]; delay_ms(1); P1=16; P2=Mang[SoThangHangDonVi]; delay_ms(1); OffLed; } break; case 3: //chinh nam { if(Back==0) { delay_ms(20); year--; if(year<=0) year=99; while(Back==0); } else if(Next==0) { delay_ms(20); year++; if(month>=100) year=0; while(Next==0); } P1=64; P2=Mang[SoNamHangChuc]; delay_ms(1); P1=128; P2=Mang[SoNamHangDonVi]; delay_ms(1); OffLed; } break; case 4: //chinh gio { if(Back==0) { delay_ms(20); hour--; if(hour<=0) hour=23; while(Back==0); } else if(Next==0) { delay_ms(20); hour++; if(hour>=24) hour=0; while(Next==0); } P1=1; P2=Mang[SoGioHangChuc]; delay_ms(1); P1=2; P2=Mang[SoGioHangDonVi]; delay_ms(1); OffLed; } break; //chinh phut case 5: { if(Back==0) { delay_ms(20); min--; if(min<=0) min=59; while(Back==0); } else if(Next==0) { delay_ms(20); min++; if(min>=60) min=0; while(Next==0); } P1=8; P2=Mang[SoPhutHangChuc]; delay_ms(1); P1=16; P2=Mang[SoPhutHangDonVi]; delay_ms(1); OffLed; } break; case 6: //chinh giay { if(Back==0) { delay_ms(20); sec--; if(sec<=0) sec=59; while(Back==0); } else if(Next==0) { delay_ms(20); sec++; if(sec>=60) sec=0; while(Next==0); } P1=64; P2=Mang[SoGiayHangChuc]; delay_ms(1); P1=128; P2=Mang[SoGiayHangDonVi]; delay_ms(1); OffLed; } break; } } } //chuong trinh chinh void main(void) { /*Read_DS1307(); //doc thoi gian tu DS1307 kt_sec=sec; //luu giay vao 1 bien kt_sec delay_ms(2000); Read_DS1307();//tre 1 khoang thoi gian sau do doc tiep kt_sec2=sec; if(kt_sec==kt_sec2) //nguoc lai neu DS1307 chua chay thi phai set thoi gian { sec=8;min=7;hour=7;day=9;month=10;year=12;date=7; Write_DS1307();// ghi du lieu vao DS1307 } */ //khoi tao cho bo dem timer TR0=1; //cho phep timer hoat dong //TH0=0xfc; //TL0=0x18; //nap gia tri dem cho thanh ghi timer 0 TR0=1; // cho phép timer 0 hoat dong TF0=0; //xoa co ngat EA=1; //cho phep ngat ET0=1; //cho phep ngat timer 0 while (1) { ScanKey(); if(Millisec>=200) { Convert_Number(); //cu 200 ms thi chuyen doi 1 lan Millisec=0; } if(kt_Timer==1&&kt_menu==0) //trong che do hien thi ngay thang nam { if(Millisec2>=500) //500ms doc thoi gian tu DS1307 1 lan { LedCPU=~LedCPU; //nhap nhay den led Millisec2=0; Read_DS1307(); //doc thoi gian tu DS1307 } Display_Ngay_Thang_Nam();//hien thi ngay thang nam len led 7 thanh } else if(kt_Timer==0&&kt_menu==0) //trong che do gio phut giay { if(Millisec2>=500) { LedCPU=~LedCPU; //nhap nhay den led Millisec2=0; Read_DS1307(); } Display_Gio_Phut_Giay(); //hien thi gioi phut giay tren led 7thanh } } } //****************************************************************************** // CAC CHUONG TRINH CON CHO I2C //****************************************************************************** void I2C_start() //ham khoi dong I2C { SCL = 1; SDA = 1; SDA = 0; SCL = 0; } void I2C_stop() { SCL = 1; SDA = 0; SDA=1; SCL=1; } unsigned char I2C_write(unsigned char dat)//ham ghi 1 byte du lieu vao DS1307 { unsigned char i; for (i=0;i<8;i++) { SDA = (dat & 0x80); ///? 1:0; SCL=1; SCL=0; dat<<=1; } SCL = 1; SCL = 0; return dat; } unsigned char I2C_read(void) { bit rd_bit; unsigned char i, dat; dat = 0x00; for(i=0;i<8;i++) /* For loop read data 1 byte */ { SCL = 1; rd_bit = SDA; /* Keep for check acknowledge */ dat = dat<<1; dat = dat | rd_bit; /* Keep bit data in dat */ SCL = 0; /* Clear SCL */ } return dat; } // CAC CHUONG TRINH CON CHO DS1307 //****************************************************************************** void DS1307_Write(unsigned char addr,unsigned char dat) { unsigned int temp; //********************************************** temp = dat ; /*HEX to BCD*/ dat = (((dat/10)*16)|(temp %10)); /*for Led 7seg*/ //********************************************** I2C_start(); /* Start i2c bus */ I2C_write(0XD0); /* Connect to DS1307 */ I2C_write(addr); /* Request RAM address on DS1307 */ I2C_write(dat); /* Connect to DS1307 for Read */ I2C_stop(); } unsigned char DS1307_read(unsigned char addr) { unsigned int temp,ret; I2C_start(); /* Start i2c bus */ I2C_write(0xD0); /* Connect to DS1307 */ I2C_write(addr); /* Request RAM address on DS1307 */ I2C_start(); /* Start i2c bus */ I2C_write(0XD1); /* Connect to DS1307 for Read */ ret = I2C_read(); /* Receive data */ I2C_stop(); //********************************************************** temp = ret; /*BCD to HEX*/ ret = (((ret/16)*10)+ (temp & 0x0f)); /*for Led 7seg*/ //********************************************************** return ret; } void Read_DS1307() { sec = DS1307_read(SEC); //doc giay tu DS1307 min = DS1307_read(MIN); hour = DS1307_read(HOUR); day = DS1307_read(DAY); date = DS1307_read(DATE); month = DS1307_read(MONTH); year = DS1307_read(YEAR); } void Write_DS1307() { DS1307_Write(SEC,sec); //ghi du lieu vao thanh ghi giay DS1307_Write(MIN,min); DS1307_Write(HOUR,hour); DS1307_Write(DAY,day); DS1307_Write(DATE,date); DS1307_Write(MONTH,month); DS1307_Write(YEAR,year); }
Comment